﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Abigail.Ui
{
    public partial class UiLogin : Form
    {
        Controller c;
        public UiLogin(Controller givenC)
        {
            InitializeComponent();
            this.Show();

            c = givenC;

            // Register eventlistener for form closing.
            this.FormClosing += UiLogin_FormClosing;
        }

        private void checkForEnterKey(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                DoLogin();
                // Stop the character from being entered into the control since it is non-numerical.
                e.Handled = true;
            }
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            DoLogin();
        }

        private void DoLogin()
        {
            Console.WriteLine("User: " + txt_initials.Text + " | Password: " + txt_password.Text);
            c.DoLogin(this, txt_initials.Text, txt_password.Text);
        }

        private void UiLogin_FormClosing(Object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason.Equals(CloseReason.UserClosing))
            {
                if (Popup.QuestionYesNo("Är du säker på att du vill avsluta?", "Avsluta", "question"))
                    Controller.ExitApplication();
                else
                    e.Cancel = true;
            }
        }
    }
}
